import WxValidate from "../../utils/wxValidate.js";
var api = require("../../utils/api");
Page({

  data: {
    region: ['吉林省', '长春市', '南关区'],
    customItem: '全部',
    array: ['厂家直供','代理商'],
    index:0
  },

  onLoad: function (options) {
    this.initValidate();
  },

  // 提示信息
  initValidate() {
    let rules = {
      company: {
        required: true
      },
      contacts: {
        required: true
      },
      phone: {
        required: true,
        tel: true,
      },
     provideClass: {
        required: true,
       
      }
    }
    let message = {
      company: {
        required: "请输入公司名称",
      },
      contacts: {
        required: "请输入联系人",
      },
      phone: {
        required: "请输入手机号",
        tel: "请输入正确手机号",
      },
      provideClass: {
        required: "请输入供应品类",
       
      }
     
    }
    this.WxValidate = new WxValidate(rules, message)
  },

  //省市区
  bindRegionChange: function (e) {
  
    this.setData({
      region: e.detail.value
    })
  },
  bindPickerChange:function(e){
    this.setData({
      index: e.detail.value,
    })
  },
  // 失败
  showModal(err) {
    wx.showModal({
      content: err.msg,
      showCancel: false
    })
  },
   //提交订单
  formSubmit:function(e){
   
    let that=this

    if (!this.WxValidate.checkForm(e)) {
      let err = this.WxValidate.errorList[0]
      this.showModal(err);
      return false;
    }

    wx.showLoading({
      title: '加载中...'
    })
    api.insertSelective1({
      cityId: wx.getStorageSync("cityId"),
      compangName: e.detail.value.company,
      createid: wx.getStorageSync("openId"),
      linkman: e.detail.value.contacts,
      linktel: e.detail.value.phone,   
      
      supplyType: e.detail.value.provideClass,
      supplyProp: Number(that.data.index) +1,  
      supplyCity: that.data.region.join(' '),  
    }).then(res => {
      if (res.result) {
        //动画隐藏
        wx.hideLoading();
        wx.showToast({
          title: '提交成功',
          icon:'none',
          success:function(){
            setTimeout(function () {
               wx.reLaunch({
                 url: '../my/my',
               })
             }, 2000);
          }
        })
      }
    })
  }
})